//
//  BROrderDetailViewController.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/28.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BROrderDetailViewController.h"
#import "BROrderHeadView.h"
#import "BRDetailHeaderCollectionViewCell.h"
#import "BRPaymentViewController.h"

#import "BROrderDetailCell.h"
#import <SDWebImage/SDWebImage.h>

@interface BROrderDetailViewController ()<QMUITableViewDelegate,QMUITableViewDataSource>
{
   
    NSString *_timeStr;
}

@property (nonatomic,strong) BROrderHeadView *headView;
@property (nonatomic,strong) BROrderDetailModel *model;

@property (nonatomic,strong) NSArray *sections;

@property (nonatomic,strong)  UIView *submitView;

@property (nonatomic, strong) dispatch_source_t timer;


@end

@implementation BROrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _timeStr = @"";
    // sections会在requestOrder后根据订单状态动态更新
    _sections = @[Localized(@"order_paymentDetail"),Localized(@"order_orderDetail"),Localized(@"home_billingRules")];
    [self setUI];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self requestOrder];
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self cancelTime];
}

- (void)dealloc
{
    [self cancelTime];
}

#pragma mark - Timer --
///增加轮训定时刷新订单金额
- (void)startQueryTime {
    
    if (self.timer) {
        return;
    }
    WS(weakSelf);
    //获得队列
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    //创建一个定时器
    self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    //设置开始时间
    dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC));
    //设置时间间隔
    uint64_t interval = (uint64_t)(5.0* NSEC_PER_SEC);
    //设置定时器
    dispatch_source_set_timer(self.timer, start, interval, 0);
    //设置回调
    dispatch_source_set_event_handler(self.timer, ^{
        [weakSelf requestOrder];
    });
    //由于定时器默认是暂停的所以我们启动一下
    //启动定时器
    dispatch_resume(self.timer);
}

- (void)cancelTime {
    if (self.timer) {
        dispatch_source_cancel(self.timer);
        NSLog(@"【取消】订单详情定时器");
        self.timer = nil;
    }
}



- (void)showUnpayUI {
    
    self.submitView.hidden = NO;
    self.tableView.frame  = CGRectMake(0, kSafeAreaTopHeight, kScreenWidth, kScreenHeight - (72 + kSafeAreaTopHeight + kSafeAreaBottomHeight));
}


#pragma mark - request --
- (void)requestOrder {
    // 使用新接口 V2，参数是 orderId（整数）
    NSInteger orderId = [self.orderNo integerValue];
    if (orderId <= 0) {
        // orderId 无效，直接返回
        self.tableView.hidden = NO;
        return;
    }
    
    NSString *url = [BRAPI OrderDetailV2:orderId];
    
    [[BRRequestManager  shared] GET:url params:nil succeed:^(NSDictionary * _Nonnull dataDict, NSInteger respCode) {
        
        // 使用新接口返回的数据结构
        self.model = [BROrderDetailModel mj_objectWithKeyValues:dataDict[@"data"]];
        
        // 更新头部显示 - 订单编号使用 uuid
        if (self.model.uuid && self.model.uuid.length > 0) {
            [self.headView.stackView br_text:self.model.uuid by:0];
        } else {
            [self.headView.stackView br_text:@"--" by:0];
        }
        
        // 使用时长显示
        NSString *durationStr = @"--";
        if (self.model.distanceTime2 && self.model.distanceTime2.length > 0) {
            durationStr = [NSString stringWithFormat:@"%@: %@",Localized(@"order_duration"), self.model.distanceTime2];
        } else if (self.model.distanceTime && self.model.distanceTime.length > 0) {
            durationStr = [NSString stringWithFormat:@"%@: %@",Localized(@"order_duration"), self.model.distanceTime];
        } else if (self.model.usingSecound > 0) {
            durationStr = [NSString stringWithFormat:@"%@: %@",Localized(@"order_duration"), [BRTools getFormatTimeFromDurationTime:(NSInteger)(self.model.usingSecound / 60)]];
        }
        [self.headView.stackView br_text:durationStr by:1];
        
        // 更新状态显示 - 使用新接口的 usingStatus
        if (self.model.usingStatus > 0) {
            self.headView.stateView.state = self.model.usingStatus;
        }
        
        // 判断是否需要显示支付按钮 - 已归还但未支付
        if (self.model.usingStatus == 2 && !self.model.pay) {
            self.status = BROrder_UNPAY;
            [self showUnpayUI];
        }
        
        // 判断是否需要轮询（使用中或已归还但未支付）
        if ((self.model.usingStatus == 1) || (self.model.usingStatus == 2 && !self.model.pay)) {
            [self startQueryTime];
        } else {
            [self cancelTime];
        }
        
        // 根据订单状态更新sections（租借中时添加租借信息section）
        [self updateSections];

        [self.tableView reloadData];
        self.tableView.hidden = NO;
        
    } failed:^(NSInteger code, NSString * _Nonnull errMsg) {
        self.tableView.hidden = NO;
    }];
}


- (void)goToPay {
    BRPaymentViewController *payVC = [[BRPaymentViewController alloc] init];
    
    // 使用新接口的 fee 作为待支付金额
    payVC.amount = self.model.fee ? self.model.fee : [NSDecimalNumber decimalNumberWithString:@"0"];
    payVC.payType = BRPAY_Rent;
    payVC.dto = self.model;
    [self.navigationController pushViewController:payVC animated:YES];
}

- (void)pop {
    
    if (self.popType == 1) {
        [self.navigationController popToRootViewControllerAnimated:YES];
        return;
    }
    [super pop];
}

- (BOOL)forceEnableInteractivePopGestureRecognizer {
    // 屏蔽右滑返回
    return self.popType != 1;

}

#pragma mark - tableView Delegate --

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    // GIF section 不显示 header
    if ([self.sections[section] isEqualToString:@"GIF_SECTION"]) {
        return 0;
    }
    return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    // GIF section 不显示 header
    if ([self.sections[section] isEqualToString:@"GIF_SECTION"]) {
        return nil;
    }
    
    UIView *head = [UIView new];
    head.backgroundColor = kColorBGF5;
    QMUILabel *title = [BRToolsView br_label:self.sections[section] textColor:kColorTitle font:[UIFont fontS:17]];
    [head addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.top.offset(14);
    }];
    
    return head;
}

/// 根据订单状态更新sections
- (void)updateSections {
    NSInteger state = self.model.usingStatus;
    NSMutableArray *mutableSections = [NSMutableArray array];
    
    // 租借中状态时，在支付信息前面添加GIF section和租借信息section
    if (state == 1) {
        [mutableSections addObject:@"GIF_SECTION"]; // GIF section 标识
        [mutableSections addObject:Localized(@"order_rentInfo")];
    }
    
    // 添加原有的sections
    [mutableSections addObject:Localized(@"order_paymentDetail")];
    [mutableSections addObject:Localized(@"order_orderDetail")];
    [mutableSections addObject:Localized(@"home_billingRules")];
    
    _sections = [mutableSections copy];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSInteger state = self.model.usingStatus;
    
    // 判断是否是GIF section（租借中状态时，section 0是GIF section）
    NSInteger gifSectionIndex = (state == 1) ? 0 : -1;
    if (indexPath.section == gifSectionIndex && [self.sections[indexPath.section] isEqualToString:@"GIF_SECTION"]) {
        // GIF section - 显示GIF图片
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GIFCell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GIFCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = kColorBGF5;
            cell.contentView.backgroundColor = kColorBGF5;
            // 移除分隔线
            cell.separatorInset = UIEdgeInsetsMake(0, kScreenWidth, 0, 0);
            
            YYAnimatedImageView *gifImageView = [[YYAnimatedImageView alloc] init];
            gifImageView.tag = 9999;
            gifImageView.contentMode = UIViewContentModeScaleAspectFit;
            gifImageView.backgroundColor = UIColor.clearColor;
            [cell.contentView addSubview:gifImageView];
            [gifImageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(cell.contentView);
                make.top.bottom.offset(0);
                make.height.mas_equalTo(250);
                make.width.mas_lessThanOrEqualTo(kScreenWidth);
            }];
        }
        
        YYAnimatedImageView *gifImageView = (YYAnimatedImageView *)[cell.contentView viewWithTag:9999];
        if (gifImageView && !gifImageView.image) {
            // 使用 YYImage 加载本地GIF文件，支持自动播放动画
            NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"shuiqiutu" ofType:@"gif"];
            if (!gifPath) {
                // 尝试从文件系统路径加载
                NSString *filePath = [[NSBundle mainBundle] pathForResource:@"shuiqiutu" ofType:@"gif" inDirectory:@"standard/Class/Order/Controller"];
                if (filePath) {
                    gifPath = filePath;
                }
            }
            if (gifPath) {
                // 使用 YYImage 加载 GIF，会自动播放动画
                YYImage *gifImage = [YYImage imageWithContentsOfFile:gifPath];
                if (gifImage) {
                    gifImageView.image = gifImage;
                }
            }
        }
        
        return cell;
    }
    
    // 判断是否是租借信息section（租借中状态时，section 1是租借信息）
    NSInteger rentInfoSectionIndex = (state == 1) ? 1 : -1;
    
    if (indexPath.section == rentInfoSectionIndex) {
        // 租借信息section
        BRODRentInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRODRentInfoCell"];
        
        // 免费时长 - 使用 ruleMinuteFree（分钟）
        NSString *freeTimeStr = @"--";
        if (self.model.ruleMinuteFree > 0) {
            freeTimeStr = [NSString stringWithFormat:@"%ld%@", (long)self.model.ruleMinuteFree, Localized(@"rule_min")];
        }
        [cell br_text:freeTimeStr by:0];
        
        // 使用时长 - 使用 usingSecound（秒）或 distanceTime2/distanceTime
        NSString *usingTimeStr = @"--";
        if (self.model.distanceTime2 && self.model.distanceTime2.length > 0) {
            usingTimeStr = self.model.distanceTime2;
        } else if (self.model.distanceTime && self.model.distanceTime.length > 0) {
            usingTimeStr = self.model.distanceTime;
        } else if (self.model.usingSecound > 0) {
            // 将秒转换为分钟显示
            NSInteger minutes = (NSInteger)(self.model.usingSecound / 60);
            usingTimeStr = [BRTools getFormatTimeFromDurationTime:minutes];
        } else if (self.model.useDuration > 0) {
            // 使用useDuration（秒）
            NSInteger minutes = (NSInteger)(self.model.useDuration / 60);
            usingTimeStr = [BRTools getFormatTimeFromDurationTime:minutes];
        }
        [cell br_text:usingTimeStr by:1];
        
        // 产生费用 - 使用全局金额格式化函数（处理百分位）
        NSDecimalNumber *fee = self.model.fee ? self.model.fee : [NSDecimalNumber decimalNumberWithString:@"0"];
        NSString *feeStr = [BRTools br_formatVietnameseDong:fee.doubleValue];
        [cell br_text:[NSString stringWithFormat:@"%@₫", feeStr] by:2];
        
        return cell;
    }
    
    // 支付信息section的索引需要根据是否有租借信息section来调整
    NSInteger paymentSectionIndex = (state == 1) ? 2 : 0;
    
    if (indexPath.section == paymentSectionIndex) {
        BRODPayDeatilCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRODPayDeatilCell"];
        
        // 使用新接口的 usingStatus
        NSInteger state = self.model.usingStatus;
        cell.cellType = state;
        
        // 产生费用 - 使用全局金额格式化函数（处理百分位）
        NSDecimalNumber *orderAmount = self.model.fee ? self.model.fee : [NSDecimalNumber decimalNumberWithString:@"0"];
        NSString *orderAmountStr = [BRTools br_formatVietnameseDong:orderAmount.doubleValue];
        [cell br_text:[NSString stringWithFormat:@"%@₫", orderAmountStr] by:0];
        
        // 押金方式 - 使用新接口的 pay_way 或 payWay
        NSString *depositMethod = @"--";
        if (self.model.pay_way && self.model.pay_way.length > 0) {
            depositMethod = self.model.pay_way;
        } else if (self.model.payWay && self.model.payWay.length > 0) {
            depositMethod = self.model.payWay;
        }
        [cell br_text:depositMethod by:1];
        
        // 押金金额 - 使用全局金额格式化函数（处理百分位）
        NSDecimalNumber *depositAmount = self.model.securityFee ? self.model.securityFee : [NSDecimalNumber decimalNumberWithString:@"0"];
        NSString *depositAmountStr = [BRTools br_formatVietnameseDong:depositAmount.doubleValue];
        [cell br_text:[NSString stringWithFormat:@"%@₫", depositAmountStr] by:2];
        
        // 实际支付金额 - 使用全局金额格式化函数（处理百分位）
        NSDecimalNumber *money = self.model.fee ? self.model.fee : [NSDecimalNumber decimalNumberWithString:@"0"];
        NSString *title = Localized(@"order_paymentAmount");
        // 已归还但未支付时，显示待支付金额（使用 fee，因为 fee 是订单费用）
        if (state == 2 && !self.model.pay) {
            title = Localized(@"order_amount2bePaid");
        }
        NSString *moneyStr = [BRTools br_formatVietnameseDong:money.doubleValue];
        [cell br_title:title text:[NSString stringWithFormat:@"%@₫", moneyStr] by:3];
        
        // 隐藏逻辑
        // 租借中时隐藏支付金额
        BOOL hiddAmount = (state == 1);
        [cell br_cellHidden:hiddAmount by:3];
        
        BOOL hiddLine = hiddAmount;
        [cell br_cellHiddenLine:hiddLine by:0];
        
        return cell;
        
    }
    
    // 订单详情section的索引需要根据是否有租借信息section来调整
    NSInteger orderDetailSectionIndex = (state == 1) ? 3 : 1;
    
    if (indexPath.section == orderDetailSectionIndex) {
        // 订单详情 - 使用新接口字段
        NSString *title1 = [NSString stringWithFormat:@"%@:",Localized(@"order_orderNumber")];
        NSString *title2 = [NSString stringWithFormat:@"%@:",Localized(@"order_rentTime")];
        NSString *title3 = [NSString stringWithFormat:@"%@:",Localized(@"order_rentLocation")];
        NSString *title4 = [NSString stringWithFormat:@"%@:",Localized(@"order_returnTime")];
        NSString *title5 = [NSString stringWithFormat:@"%@:",Localized(@"order_returnLocation")];
        
        // 订单编号 - 使用新接口的 uuid
        NSString *orderNoStr = self.model.uuid ? self.model.uuid : @"--";
        NSString *text1 = [NSString stringWithFormat:@"%@ %@",title1, orderNoStr];
        
        // 租借时间 - 使用新接口的 borrowTime 或 createTime（时间戳）
        NSString *rentTimeStr = @"--";
        if (self.model.borrowTime > 0) {
            rentTimeStr = [BRTools getTimeFromTimestamp:self.model.borrowTime formatter:@"yyyy-MM-dd HH:mm:ss"];
        } else if (self.model.createTime > 0) {
            rentTimeStr = [BRTools getTimeFromTimestamp:self.model.createTime formatter:@"yyyy-MM-dd HH:mm:ss"];
        }
        NSString *text2 = [NSString stringWithFormat:@"%@ %@",title2, rentTimeStr];
        
        // 租借地点 - 使用新接口的 box1SiteName
        NSString *rentLocation = self.model.box1SiteName ? self.model.box1SiteName : @"--";
        NSString *text3 = [NSString stringWithFormat:@"%@ %@",title3, rentLocation];
        
        NSAttributedString *att1 = [BRStringTools br_att:text1 rang:NSMakeRange(title1.length, text1.length - title1.length) textC:kColorTitle];
        NSAttributedString *att2 = [BRStringTools br_att:text2 rang:NSMakeRange(title2.length, text2.length - title2.length) textC:kColorTitle];
        NSAttributedString *att3 = [BRStringTools br_att:text3 rang:NSMakeRange(title3.length, text3.length - title3.length) textC:kColorTitle];
        
        // 判断是否有归还信息 - 使用新接口的 usingStatus
        NSInteger state = self.model.usingStatus;
        BOOL hasReturnInfo = (self.model.returnTime > 0) && state == 2;
        
        if (!hasReturnInfo) {
            // 无归还信息 - 只显示租借信息
            BRODRentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRODRentCell"];
            
            [cell.topStack br_attText:att1 by:0];
            [cell.topStack br_attText:att2 by:1];
            [cell.topStack br_attText:att3 by:2];
            
            // 柜机SN - 参考订单列表的取值逻辑
            NSString *title6 = [NSString stringWithFormat:@"%@:",Localized(@"order_cabinetSN")];
            NSString *cabinetSN = self.model.box1DeviceId && self.model.box1DeviceId.length > 0 ? self.model.box1DeviceId : @"";
            if (cabinetSN.length == 0 && self.model.box1DeviceUuid && self.model.box1DeviceUuid.length > 0) {
                cabinetSN = self.model.box1DeviceUuid;
            }
            if (cabinetSN.length == 0 && self.model.singleDeviceUuid && self.model.singleDeviceUuid.length > 0) {
                cabinetSN = self.model.singleDeviceUuid;
            }
            if (cabinetSN.length == 0 && self.model.rentCabinetNo && self.model.rentCabinetNo.length > 0) {
                cabinetSN = self.model.rentCabinetNo;
            }
            NSString *cabinetSNValue = cabinetSN.length > 0 ? cabinetSN : @"--";
            NSString *text6 = [NSString stringWithFormat:@"%@ %@",title6, cabinetSNValue];
            NSAttributedString *att6 = [BRStringTools br_att:text6 rang:NSMakeRange(title6.length, text6.length - title6.length) textC:kColorTitle];
            
            // 充电宝SN - 使用新接口的 singleDeviceUuid
            NSString *title7 = [NSString stringWithFormat:@"%@:",Localized(@"order_powerBankSN")];
            NSString *pbSN = self.model.singleDeviceUuid ? self.model.singleDeviceUuid : @"--";
            NSString *text7 = [NSString stringWithFormat:@"%@ %@",title7, pbSN];
            NSAttributedString *att7 = [BRStringTools br_att:text7 rang:NSMakeRange(title7.length, text7.length - title7.length) textC:kColorTitle];
            
            [cell.bottomStack br_attText:att6 by:0];
            [cell.bottomStack br_attText:att7 by:1];
            
            return cell;
        }
    
    // 有归还信息
        NSString *returnTimeStr = @"--";
        if (self.model.returnTime > 0) {
            returnTimeStr = [BRTools getTimeFromTimestamp:self.model.returnTime formatter:@"yyyy-MM-dd HH:mm:ss"];
        }
        NSString *text4 = [NSString stringWithFormat:@"%@ %@",title4, returnTimeStr];
        
        // 归还地点 - 使用新接口的 box2SiteName
        NSString *returnLocation = self.model.box2SiteName ? self.model.box2SiteName : @"--";
        NSString *text5 = [NSString stringWithFormat:@"%@ %@",title5, returnLocation];
        
        NSAttributedString *att4 = [BRStringTools br_att:text4 rang:NSMakeRange(title4.length, text4.length - title4.length) textC:kColorTitle];
        NSAttributedString *att5 = [BRStringTools br_att:text5 rang:NSMakeRange(title5.length, text5.length - title5.length) textC:kColorTitle];

        BRODLostCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRODLostCell"];
        
        [cell.topStack br_attText:att1 by:0];
        [cell.topStack br_attText:att2 by:1];
        [cell.topStack br_attText:att3 by:2];
        
        [cell.centStack br_attText:att4 by:0];
        [cell.centStack br_attText:att5 by:1];
        [cell.centStack br_textHidden:YES by:2];
        
        cell.centView.hidden = NO;
        
        // 租借设备信息：柜机SN - 参考订单列表的取值逻辑
        NSString *title6 = [NSString stringWithFormat:@"%@:",Localized(@"order_cabinetSN")];
        NSString *cabinetSN = self.model.box1DeviceId && self.model.box1DeviceId.length > 0 ? self.model.box1DeviceId : @"";
        if (cabinetSN.length == 0 && self.model.box1DeviceUuid && self.model.box1DeviceUuid.length > 0) {
            cabinetSN = self.model.box1DeviceUuid;
        }
        if (cabinetSN.length == 0 && self.model.singleDeviceUuid && self.model.singleDeviceUuid.length > 0) {
            cabinetSN = self.model.singleDeviceUuid;
        }
        if (cabinetSN.length == 0 && self.model.rentCabinetNo && self.model.rentCabinetNo.length > 0) {
            cabinetSN = self.model.rentCabinetNo;
        }
        NSString *rentCabinetSN = cabinetSN.length > 0 ? cabinetSN : @"--";
        NSString *text6 = [NSString stringWithFormat:@"%@ %@",title6, rentCabinetSN];
        NSAttributedString *att6 = [BRStringTools br_att:text6 rang:NSMakeRange(title6.length, text6.length - title6.length) textC:kColorTitle];
        
        // 充电宝SN
        NSString *title7 = [NSString stringWithFormat:@"%@:",Localized(@"order_powerBankSN")];
        NSString *pbSN = self.model.singleDeviceUuid ? self.model.singleDeviceUuid : @"--";
        NSString *text7 = [NSString stringWithFormat:@"%@ %@",title7, pbSN];
        NSAttributedString *att7 = [BRStringTools br_att:text7 rang:NSMakeRange(title7.length, text7.length - title7.length) textC:kColorTitle];
        
        [cell.bottomStack br_attText:att6 by:0];
        [cell.bottomStack br_attText:att7 by:1];
        
        return cell;
        
        
        
//        BRODCompletedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRODCompletedCell"];
//
//        [cell.topStack br_attText:att1 by:0];
//        [cell.topStack br_attText:att2 by:1];
//        [cell.topStack br_attText:att3 by:2];
//
//        [cell.centStack br_attText:att4 by:0];
//        [cell.centStack br_attText:att5 by:1];
//        [cell.centStack br_attText:att6 by:2];
//
//        return cell;
    }
    
    BRODBillingRuleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BRODBillingRuleCell"];
    
    // 使用新接口的计费规则字段构建规则文本，使用全局金额格式化函数（处理百分位）
    NSString *ruleText = @"--";
    if (self.model.ruleMinuteFree > 0 || self.model.ruleMinuteCycle > 0 || (self.model.ruleDayFee && self.model.ruleDayFee.doubleValue > 0)) {
        NSMutableString *rule = [NSMutableString string];
        if (self.model.ruleMinuteCycle > 0 && self.model.ruleCycleFee) {
            // 使用全局金额格式化函数格式化 ruleCycleFee
            NSString *formattedCycleFee = [BRTools br_formatVietnameseDong:self.model.ruleCycleFee.doubleValue];
            [rule appendFormat:@"%@₫/%d%@", formattedCycleFee, (int)self.model.ruleMinuteCycle, Localized(@"rule_min")];
        }
        if (self.model.ruleDayFee && self.model.ruleDayFee.doubleValue > 0) {
            if (rule.length > 0) {
                [rule appendString:@","];
            }
            // 使用全局金额格式化函数格式化 ruleDayFee
            NSString *formattedDayFee = [BRTools br_formatVietnameseDong:self.model.ruleDayFee.doubleValue];
            [rule appendFormat:@"%@%@₫", Localized(@"rule_24hours"), formattedDayFee];
        }
        ruleText = rule.length > 0 ? rule : @"--";
    }
    
    cell.ruleL.text = ruleText;
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}


#pragma mark - SET UI --
- (void)setUI {
    
    [self br_createTop:Localized(@"order_orderDetail")];
//    self.tableType = 1;
    self.tableView.estimatedSectionHeaderHeight = 100;
    
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.backgroundColor = kColorBGF5;
//    self.tableView.rowHeight = 144;
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    [self.tableView registerClass:[BRODPayDeatilCell class] forCellReuseIdentifier:@"BRODPayDeatilCell"];
    [self.tableView registerClass:[BRODRentInfoCell class] forCellReuseIdentifier:@"BRODRentInfoCell"];
    [self.tableView registerClass:[BRODRentCell class] forCellReuseIdentifier:@"BRODRentCell"];
    [self.tableView registerClass:[BRODLostCell class] forCellReuseIdentifier:@"BRODLostCell"];
    [self.tableView registerClass:[BRODCompletedCell class] forCellReuseIdentifier:@"BRODCompletedCell"];
    [self.tableView registerClass:[BRODBillingRuleCell class] forCellReuseIdentifier:@"BRODBillingRuleCell"];
    
    self.tableView.hidden = YES;
    
    if (@available(iOS 15.0, *)) {
        
        self.tableView.sectionHeaderTopPadding = 0;
        
    }
    
    self.tableView.tableHeaderView = self.headView;
    UIView *foot = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
    self.tableView.tableFooterView = foot;
   
    UIView *submitView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - (kSafeAreaBottomHeight + 72), kScreenWidth, (kSafeAreaBottomHeight + 72) )];
    submitView.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:submitView];
    [submitView br_addBaseShadow];
    submitView.hidden = YES;
    _submitView = submitView;
    
    QMUIButton *submitBtn = [BRToolsView br_btnBGImg:Localized(@"home_gotoPay")];
    
    [submitView addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(52);
        make.width.mas_equalTo(kScreenWidth - 28);
        make.centerX.equalTo(self.view);
        make.top.equalTo(submitView.mas_top).offset(15);
    }];
    [submitBtn addTarget:self action:@selector(goToPay) forControlEvents:UIControlEventTouchUpInside];
}

- (BROrderHeadView *)headView {
    if (_headView == nil) {
        _headView = [[BROrderHeadView alloc] init];
    }
    return _headView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
